/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/idenger">idenger</a> All rights reserved.
 */
package com.idenger.modules.edu.web.banner;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.idenger.common.web.BaseController;
import com.idenger.modules.edu.entity.common.BaseResponseMsg;
import com.idenger.modules.edu.entity.common.ResponseMsg;
import com.idenger.modules.edu.service.banner.EduBannerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.idenger.common.config.Global;
import com.idenger.common.persistence.Page;
import com.idenger.common.utils.StringUtils;
import com.idenger.modules.edu.entity.banner.EduBanner;

/**
 * bannerController
 * @author 兰正军
 * @version 2017-11-27
 */
@Controller
@RequestMapping(value = "${adminPath}/edu/banner/eduBanner")
public class EduBannerController extends BaseController {
	@Autowired
	private EduBannerService eduBannerService;
	
	@ModelAttribute
	public EduBanner get(@RequestParam(required=false) String id) {
		EduBanner entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = eduBannerService.get(id);
		}
		if (entity == null){
			entity = new EduBanner();
		}
		return entity;
	}
	
	@RequiresPermissions("edu:banner:eduBanner:view")
	@RequestMapping(value = {"list", ""})
	public String list(EduBanner eduBanner, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<EduBanner> page = eduBannerService.findPage(new Page<EduBanner>(request, response), eduBanner); 
		model.addAttribute("page", page);
		return "modules/edu/banner/eduBannerList";
	}

	@RequiresPermissions("edu:banner:eduBanner:view")
	@RequestMapping(value = "form")
	public String form(EduBanner eduBanner, Model model) {
		model.addAttribute("eduBanner", eduBanner);
		return "modules/edu/banner/eduBannerForm";
	}

	@RequiresPermissions("edu:banner:eduBanner:edit")
	@RequestMapping(value = "save")
	public String save(EduBanner eduBanner, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, eduBanner)){
			return form(eduBanner, model);
		}
		List<EduBanner> list = eduBannerService.findList(eduBanner);
		if(10<=list.size()){
			addMessage(redirectAttributes, "轮播图最多只能十条");
			return "redirect:"+Global.getAdminPath()+"/edu/banner/eduBanner/?repage";
		}
		eduBanner.setType("0");
		eduBannerService.save(eduBanner);
		addMessage(redirectAttributes, "保存banner图成功");
		return "redirect:"+Global.getAdminPath()+"/edu/banner/eduBanner/?repage";
	}
	
	@RequiresPermissions("edu:banner:eduBanner:edit")
	@RequestMapping(value = "delete")
	public String delete(EduBanner eduBanner, RedirectAttributes redirectAttributes) {
		eduBannerService.delete(eduBanner);
		addMessage(redirectAttributes, "删除banner图成功");
		return "redirect:"+Global.getAdminPath()+"/edu/banner/eduBanner/?repage";
	}
	
	@RequestMapping(value = "get")
	public String get(EduBanner eduBanner, HttpServletRequest request, HttpServletResponse response, Model model){
		BaseResponseMsg baseResponseMsg;
		List<EduBanner> list = eduBannerService.findBannerForPlace(eduBanner.getPlace());
		if(list.size()>0){
			baseResponseMsg = new BaseResponseMsg(new ResponseMsg("1", "error"));
			baseResponseMsg.addData(true);
			return renderString(response, baseResponseMsg);
		}
		baseResponseMsg = new BaseResponseMsg(new ResponseMsg("0", "success"));
		baseResponseMsg.addData(false);
		return renderString(response, baseResponseMsg);
	}
	
}